package com.lxu.service;

import com.lxu.util.InsertRowUtils;
import com.lxu.util.WorderCombineUtils;
import com.lxu.util.WorderToNewWordUtils;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.springframework.stereotype.Service;

import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("wew19")
public class WordService {

    /**
     * 不合并文件 仅仅填充模板
     */
    public void fillWordTemplate() throws Exception {

        InputStream in = this.getClass().getResourceAsStream("/templates/担保业务初审批量立项表（金融机构）表头.docx");
        exportWord(in, "result.docx");
    }

    /**
     * 从表格中间开始，新增行
     *
     * @throws Exception
     */
    public void insertRows() throws Exception {
        InputStream in = this.getClass().getResourceAsStream("/templates/担保业务初审批量立项表（金融机构）表头-3.docx");
        XWPFDocument hwpf = new XWPFDocument(in);
        List<XWPFTable> tables = hwpf.getTables();//获取word中所有的表格

        FileOutputStream out = new FileOutputStream("D:\\middleTable.docx");
        XWPFTable table = tables.get(0);//获取第一个表格

        InsertRowUtils.insertRow(table, 3, 2);//此方法在下方，直接复制拿走

        hwpf.write(out);
        out.flush();
        out.close();
    }

    /**
     * 获取文档中的表格列表
     *
     * @return
     */
    public List<XWPFTable> XWPFTables(InputStream in) throws IOException {
        XWPFDocument hwpf = new XWPFDocument(in);
        return hwpf.getTables();
    }


    /**
     * 填充表格，从中间某个位置开始追加，而不是从最后追加
     */
    public void fillWordTemplate4TableMiddle() throws Exception {
        InputStream in = this.getClass().getResourceAsStream("/templates/担保业务初审批量立项表（金融机构）表头-3.docx");
        XWPFDocument hwpf = new XWPFDocument(in);
        List<XWPFTable> tables = hwpf.getTables();//获取word中所有的表格

        FileOutputStream out = new FileOutputStream("D:\\middleTable.docx");
        XWPFTable table = tables.get(0);//获取第一个表格

        InsertRowUtils.insertAndCopyRow(table, 3, 2);//此方法在下方，直接复制拿走

        hwpf.write(out);
        out.flush();
        out.close();
    }


    /**
     * 多模板填充，填充后合并到一个word中
     */
    public void fillMultipleWordTemplateAndCombine() throws Exception {

        InputStream in = this.getClass().getResourceAsStream("/templates/担保业务初审批量立项表（金融机构）表头-1.docx");
        exportWord(in, "result1.docx");

        InputStream in2 = this.getClass().getResourceAsStream("/templates/担保业务初审批量立项表（金融机构）表头-2.docx");
        exportWord(in2, "result2.docx");

    }

    private void exportWord(InputStream in, String fileName) throws Exception {
        //添加业务数据
        Map<String, String> businessData = generateBusinessData();
        Map<String, List<List<String>>> tableData = generateTableData();
        Map<String, Integer> headers = getTableRowsData();
        XWPFDocument docx = WorderToNewWordUtils.changWordForComplex(in, businessData, tableData, headers, new HashMap<String, List<String>>());
        OutputStream out = new FileOutputStream(new File("D:\\" + fileName));
        docx.write(out);
    }

    private Map<String, Integer> getTableRowsData() {
        Map<String, Integer> headers = new HashMap<String, Integer>();
        headers.put("table0", 2);
        return headers;
    }

    private Map<String, String> generateBusinessData() {
        Map<String, String> map = new HashMap<String, String>();
        map.put("no", "001");
        map.put("date", "2021-09-01");
        map.put("productName", "农担公司金融产品");
        map.put("orgName", "中国建设银行");
        map.put("govName", "合肥市人民政府");
        map.put("company", "天勤盛创");
        map.put("myRate", "50");
        map.put("finaceRate", "40");
        map.put("otherRate", "10");
        map.put("mScope", "1-100");
        map.put("customerType", "自然人");
        map.put("sxType", "不知道");
        map.put("contractType", "字数比较多，吱吱吱吱吱吱吱吱吱吱吱吱，多打点字啊。。。。。。。。。。。。。。");
        map.put("riskDesc", "风险风险风险风险风险风险风险风险");
        map.put("bank", "中国建设银行");
        map.put("area", "合肥市");
        map.put("totalP", "10");
        map.put("totalM", "1000");
        map.put("agent", "合肥市长");
        map.put("detailOrg", "安徽省天勤盛创股份有限公司");
        return map;
    }

    private Map<String, List<List<String>>> generateTableData() {
        Map<String, List<List<String>>> map = new HashMap<String, List<List<String>>>();
        List<String> cols = new ArrayList<String>();
        for (int i = 0; i < 13; i++) {
            cols.add("zhangsan" + i);
        }
        List<List<String>> rows = new ArrayList<List<String>>();
        for (int j = 0; j < 5; j++) {
            rows.add(cols);
        }
        map.put("table0", rows);
        return map;
    }
}
